//
//  UserAccontModel.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/19.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
    /**
     {
     "access_token": "ACCESS_TOKEN",
     "expires_in": 1234,
     "remind_in":"798114",
     "uid":"12341234"
     }
     */
    
    var uid: String?
    // 过期时间
    // -[__NSCFNumber length] - unrecognized selector sent to instance 0xb000000000229c43'
    // 代表属性的类型写错啦。把 Int 写成 String 了
    var expires_in: NSTimeInterval = 0 {
    didSet{
    // 计算过期时间
    expiresDate = NSDate(timeIntervalSinceNow: expires_in)
    }
    }
    //    var expires_in: String?  --> 会崩溃
    
    var access_token: String?
    
    var remind_in: String?
    
    //过期时间
     var expiresDate: NSDate?
    
    var name: String?
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?

    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    required init?(coder decoder: NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        expires_in = decoder.decodeDoubleForKey("expires_in")
        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        name = decoder.decodeObjectForKey("name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate

    }
    
    // 归档要调用的方法 --> 如何保存
    func encodeWithCoder(encoder: NSCoder) {
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(name, forKey: "name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")

    }
    
}

